import random
from dlgo.agent.base import Agent
from dlgo.agent.helpers import is_point_an_eye
from dlgo.gotypes import Point
from dlgo.goboard_slow import Move

class RandomBot(Agent):
    """
    随机机器人类
    """
    def select_move(self, game_state):
        """随机选择一个位置
        :param game_state: 棋盘状态
        :return:返回一个行为
        首先一个循环来把所有合法且不会填自己的眼的所有棋子枚举出来，然后用随机函数随机选择一个位置来下，如果一个满足的位置都没有就跳过
        """
        candidates = []
        for r in range(1, game_state.board.num_rows + 1):
            for c in range(1, game_state.board.num_cols + 1):
                candidate = Point(row=r, col=c)
                if (
                    game_state.is_valid_move(Move.play(candidate)) and
                    not is_point_an_eye(game_state.board, candidate, game_state.next_player)
                ):
                    candidates.append(candidate)
        if not candidates:
            return Move.pass_turn()
        return Move.play(random.choice(candidates))